package com.whut.gkd.controller;

import com.whut.gkd.bean.ApplyGood;
import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.service.IApplyGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("apply")
public class ApplyGoodController {

    @Autowired
    private IApplyGoodService applyGoodService;

    @RequestMapping("findAllApplyGood.do")
    public ModelAndView findAllApplyGood(@RequestParam(defaultValue = "1") int currentPage, String goodName,
                                         @RequestParam(defaultValue = "0") int type, HttpSession session){

        //判断请求类型，若为1，就是搜索，若为2就是全部商品，若为0，就是搜索翻页
        if(type==1) {
            session.setAttribute("applyGoodName", goodName);
        }else if(type==0){
            goodName = (String)session.getAttribute("applyGoodName");
        }else if(type==2){
            session.removeAttribute("applyGoodName");
        }
        ModelAndView modelAndView = new ModelAndView();
        //获得用户的店铺Id，然后搜索所有的商品
        //判断是否为商家
        ShopInfo shopInfo = (ShopInfo) session.getAttribute("shopinfo");
        if(shopInfo!=null) {
            int shopId = shopInfo.getShopId();
            PageInfo<ApplyGood> pageInfo = applyGoodService.findAllApplyGood(shopId,currentPage, goodName);
            modelAndView.addObject("pageInfo",pageInfo);
        }
        //根据商铺Id获得用户申请的商品
        modelAndView.setViewName("applyGood-list");
        return modelAndView;
    }

    @RequestMapping("addApplyGood.do")
    public String addApplyGood(ApplyGood applyGood, HttpSession session){
        //这里可以判断也可以不判断是否用户是商家，能使用此处用户的用户肯定是商家
        ShopInfo shopInfo = (ShopInfo) session.getAttribute("shopinfo");
        applyGood.setShopId(shopInfo.getShopId());
        applyGood.setShopName(shopInfo.getShopName());
        applyGoodService.addApplyGood(applyGood);
        return "redirect:findAllApplyGood.do";
    }

    @RequestMapping("deleteApplyGoodByApplyId.do")
    public String deleteApplyGoodByApplyId(int applyId){
        applyGoodService.deleteApplyGoodByApplyId(applyId);
        return "redirect:findAllApplyGood.do";
    }

    @RequestMapping("toModifyApplyGood.do")
    public ModelAndView toModifyApplyGood(int applyId){
        ApplyGood applyGood = applyGoodService.findApplyGoodByApplyId(applyId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("applyGood",applyGood);
        modelAndView.setViewName("applyGood-update");
        return modelAndView;
    }

    @RequestMapping("modifyApplyGood.do")
    public String modifyApplyGood(ApplyGood applyGood){
        applyGoodService.modifyApplyGood(applyGood);
        return "redirect:findAllApplyGood.do";
    }

    @RequestMapping("deleteAllApplyGood.do")
    public String deleteAllApplyGood(String applyIds){
        String[] idsStr = applyIds.split(",");
        List<Integer> ids = new ArrayList<>();
        for(String s:idsStr){
            ids.add(Integer.parseInt(s));
        }
        applyGoodService.deleteAllApplyGood(ids);
        return "redirect:findAllApplyGood.do";
    }


    @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage, String shopName,
                                @RequestParam(defaultValue = "0") int type,
                                HttpSession session){
//        int applyid;
//        if(applyId == null){
//            applyid = 0;
//        }else{
//            applyid = Integer.valueOf(applyId);
//        }
//        //转换为int
//        Integer integer = new Integer(applyid);
//        int apply = integer.intValue();

        if(type==1) {
            session.setAttribute("shopName", shopName);
        }else if(type==0){
            if(session.getAttribute("shopName") != null){
                shopName= (String) session.getAttribute("shopName");
            }
        }else if(type==2){
        }
        PageInfo<ApplyGood> pageInfo=applyGoodService.findAll(currentPage,shopName);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("goodsmanager");
        mv.addObject("pageInfo",pageInfo);
        //mv.addObject("objList",pageInfo.getList());
        return mv;
    }


    @RequestMapping("allowgoodByapplyId.do")
    public String allowgoodByapplyId(int applyId){
        applyGoodService.allowgoodByapplyId(applyId);
        applyGoodService.deleteByapplyId(applyId);
        return  "redirect:findAll.do";
    }

    @RequestMapping("rejectgoodByapplyId.do")
    public String rejectgoodByapplyId(int applyId){

        applyGoodService.deleteByapplyId(applyId);
        return  "redirect:findAll.do";
    }
}

